home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / gfx / conv / ilbm24.lha / ilbm / packer.c < prev    next >
C/C++ Source or Header  |  1993-02-02  |  3KB  |  127 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  * 
  14.  * Slightly modified by Brett Van Sprewenburg, Eastman Kodak.
  15.  * It will now accept only a pointer to the image data, not pointers to pointers.
  16.  *
  17.  *----------------------------------------------------------------------*/
  18. #include <packer.h>
  19.  
  20. static byte *PutDump(byte *, int);
  21. static byte *PutRun(byte *,int,int);
  22.  
  23. #define DUMP    0
  24. #define RUN    1
  25.  
  26. #define MinRun 3    
  27. #define MaxRun 128
  28. #define MaxDat 128
  29.  
  30. /* When used on global definitions, static means private.
  31.  * This keeps these names, which are only referenced in this
  32.  * module, from conficting with same-named objects in your program.
  33.  */ 
  34. static int putSize;
  35. static byte buf[128];    /* [TBD] should be 128?  on stack?*/
  36.  
  37. #define GetByte()    (*source++)
  38. #define PutByte(c)    { *dest++ = (c);   ++putSize; }
  39.  
  40.  
  41. static byte *PutDump(byte *dest, int nn)
  42.     {
  43.     int i;
  44.  
  45.     PutByte(nn-1);
  46.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  47.     return(dest);
  48.     }
  49.  
  50. static byte *PutRun(byte *dest, int nn, int cc)
  51.     {
  52.     PutByte(-(nn-1));
  53.     PutByte(cc);
  54.     return(dest);
  55.     }
  56.  
  57. #define OutDump(nn)   dest = PutDump(dest, nn)
  58. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  59.  
  60. /*----------- packrow --------------------------------------------------*/
  61. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  62.  * destination pointers.  RETURNs count of packed bytes.
  63.  */
  64. long packrow(byte *pSource, byte *pDest, long rowSize)
  65.     {
  66.     byte *source, *dest;
  67.     byte c;
  68.     char lastc = '\0';
  69.     int mode = DUMP;
  70.     int nbuf = 0;        /* number of chars in buffer */
  71.     int rstart = 0;        /* buffer index current run starts */
  72.  
  73.     source = pSource;
  74.     dest = pDest;
  75.     putSize = 0;
  76.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  77.     nbuf = 1;   rowSize--;    /* since one byte eaten.*/
  78.  
  79.  
  80.     for (;  rowSize;  --rowSize) {
  81.     buf[nbuf++] = c = GetByte();
  82.     switch (mode) {
  83.         case DUMP: 
  84.             /* If the buffer is full, write the length byte,
  85.                then the data */
  86.             if (nbuf>MaxDat) {
  87.                 OutDump(nbuf-1);  
  88.                 buf[0] = c; 
  89.                 nbuf = 1;   rstart = 0; 
  90.                 break;
  91.                 }
  92.  
  93.             if (c == lastc) {
  94.                 if (nbuf-rstart >= MinRun) {
  95.                 if (rstart > 0) OutDump(rstart);
  96.                 mode = RUN;
  97.                 }
  98.                 else if (rstart == 0)
  99.                 mode = RUN;    /* no dump in progress,
  100.                 so can't lose by making these 2 a run.*/
  101.                 }
  102.             else  rstart = nbuf-1;        /* first of run */ 
  103.             break;
  104.  
  105.         case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  106.                 /* output run */
  107.                OutRun(nbuf-1-rstart,lastc);
  108.                 buf[0] = c;
  109.                 nbuf = 1; rstart = 0;
  110.                 mode = DUMP;
  111.                 }
  112.             break;
  113.         }
  114.  
  115.     lastc = c;
  116.     }
  117.  
  118.     switch (mode) {
  119.     case DUMP: OutDump(nbuf); break;
  120.     case RUN: OutRun(nbuf-rstart,lastc); break;
  121.     }
  122.     pSource = source;
  123.     pDest = dest;
  124.     return(putSize);
  125.     }
  126.  
  127.